package com.cmcc.qx.util;

import java.text.SimpleDateFormat;
import java.util.Date;

import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

/**
 * 自定义Json处理器
 * 
 * @author Jason
 * 
 */
public class JsonValueProcessorImpl implements JsonValueProcessor {

	private String fmt = "yyyy-MM-dd";

	public JsonValueProcessorImpl() {
		super();
	}

	public JsonValueProcessorImpl(String fmt) {
		this();
		this.fmt = fmt;
	}

	public Object processArrayValue(Object value, JsonConfig cfg) {
		String[] obj = {};
		if (value != null) {
			if (value instanceof Date[]) {
				SimpleDateFormat sdf = new SimpleDateFormat(fmt);
				Date[] dates = (Date[]) value;
				obj = new String[dates.length];
				for (int i = 0; i < dates.length; i++) {
					obj[i] = sdf.format(dates[i]);
				}
			}
		}
		return obj;
	}

	public Object processObjectValue(String key, Object value, JsonConfig cfg) {
		if (value == null) {
			return "";
		}
		if (value instanceof Date) {
			SimpleDateFormat sdf = new SimpleDateFormat(fmt);
			return sdf.format((Date) value);
		}
		return value.toString();
	}

}
